<template>
  <div class="app-container">
  <div class="filter-container">
        <el-button type="primary"  @click="addClick()" style="margin-left:0px;">新增</el-button>
        <el-input clearable v-model="search.number" placeholder="配件名称" style="width: 170px;margin-left:10px;"></el-input>
        <el-date-picker v-model="search.daterange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="margin-left:10px;">
        </el-date-picker>

        <el-button type="primary" icon="el-icon-search" @click="searchClick()" style="margin-left:10px;">查询</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" 
      border fit highlight-current-row stripe  style="width: 100%">
      <el-table-column align="center" label="序" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row._id }}</span>
        </template>
      </el-table-column>
      
     <el-table-column  min-width="100" align="center" label="类别">
        <template slot-scope="scope">
          <span>{{ scope.row.number }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="品牌">
        <template slot-scope="scope">
          <span>{{ scope.row.partcode }}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="100" align="center" label="计量单位">
        <template slot-scope="scope">
          <span>{{ scope.row.partcode }}</span>
        </template>
      </el-table-column>
      
      <el-table-column  min-width="100" align="center" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.state  }}</span>
        </template>
      </el-table-column>
      
      <el-table-column  min-width="100" align="center" label="日期">
        <template slot-scope="scope">
          <span>{{ scope.row.createtime | parseTime('{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      
      <el-table-column align="center" label="操作"  width="280" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="tableRowClick().enable(scope.row.id)">启用/禁用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.rows" @pagination="fetchData" />


<!-- 新增 -->
<el-dialog :title="dialogAdd.title" :visible.sync="dialogAdd.visible"  width="900px">
    <div class="ci">
        <div class="item">
            <span class="name">类别：</span>
            <span class="value">
                <el-input clearable v-model="dialogAdd.data.parttype" style="width:220px;"></el-input>
            </span>
        </div>
        <div class="item">
            <span class="name">品牌：</span>
            <span class="value">
                <el-input clearable v-model="dialogAdd.data.partcode" style="width:220px;"></el-input>
            </span>
        </div>
        <div class="item">
            <span class="name">计量单位：</span>
            <span class="value">
                <el-input clearable v-model="dialogAdd.data.count" style="width:220px;"></el-input>
            </span>
        </div>
       
        
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button   @click="dialogAdd.visible = false">关 闭</el-button>
        <el-button  type="primary" @click="dialogAddClick().save()">保 存</el-button>
    </div>
</el-dialog>
<!-- end 新增 -->



  </div>
</template>

<script>
import * as partsApi from '@/api/finance/parts'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import * as enumHelper from '@/utils/enum'
import {objectMerge,objectEmpty} from '@/utils/index'
import Print from '@/utils/print'

let printHandler;

export default {
    components: { Pagination },
    data(){
        return{
            search:{ 
                number:'',
                customer:'',
                phone:'',
                daterange:'',
            },
            listQuery: {
                page: 1,
                rows: 20,
                name:'',
                phone:'',
                state:-1,
            },
            list:null,
            listLoading:false,
            total:0,
            selectOptions:{
                takers:[]
            },

            
            dialogAdd:{
                visible:false,
                saveloading:false,
                title:'新增',
                data:{
                    id:'',
                    parttype:'',
                    partname:'',
                    count:'',
                    storename:'',
                    storeplace:'',
                }
            },
        }
    },
    created(){
        var that=this;
        //初始化下拉菜单
        //enumHelper.initOptions(that.selectOptions.salesState,enumHelper.presale.salesState);
        that.fetchData();
    },
    filters:{
      parseFixType(val){
        return enumHelper.aftersale.fixType.getName(val);
      },
      parseFixCategory(val){
        return enumHelper.aftersale.fixCategory.getName(val);
      },
      parseClaimdamageState(val){
        return enumHelper.aftersale.claimdamageState.getName(val);
      }

    },
    methods:{
        fetchData(){
          var that=this;
            partsApi.getPartspurchaseList({}).then(res=>{
              if(res.data){
                that.list=[...res.data.items.map((item,index)=>{
                  item._id=index+1;
                  return item;
                })];
                that.total=res.data.total;
              }
            })
        },
        refreshClick(){
          this.fetchData();
        },
        addClick(){
          var that=this;
            that.dialogAddClick().init();
            that.dialogAdd.visible=true;
        },

        tableRowClick(){
            var that=this;
            return {
                
                enable(id){
                    var idx=that.list.findIndex(o=>o.id==id);
                    that.list[idx].state=!that.list[idx].state;
                },

            }
        },
        
        dialogAddClick(){
            var that=this;
            var dg=that.dialogAdd;
            return {
                init(data){
                    data?objectMerge(dg.data,data):objectEmpty(dg.data);
                },
                save(){
                    dg.visible=false;
                }
            }
        },
        

    }
}
</script>

<style>

</style>